package org.ztr.aiollama.repository;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/9/20 20:28
 * @Description: 聊天记录存储
 * @Version: 1.0
 */
@Repository
public class InMemoryChatHistoryRepository implements ChatHistoryRepository{
    private static final Logger logger = LogManager.getLogger(InMemoryChatHistoryRepository.class);

    private Map<String, List<String>> chatHistory = new HashMap<>();


    @Override
    public void save(String type, String chatId) {
//        if (!chatHistory.containsKey(type)){
//            chatHistory.put(type, new ArrayList<>());
//        }
//        List<String> chatIds = chatHistory.get(type);
        List<String> chatIds = chatHistory.computeIfAbsent(type, k -> new ArrayList<>());
        if (chatIds.contains(chatId)){
            return;
        }
        chatIds.add(chatId);
    }

    @Override
    public List<String> getChatIds(String type) {
//        List<String> chatIds = chatHistory.get(type);
//        return chatIds != null ? chatIds : new ArrayList<>();
        return chatHistory.getOrDefault(type, List.of());
    }
}
